package com.digitalchina.lxnsy.update;

import android.app.Activity;
import android.app.Dialog;
import androidx.annotation.NonNull;
import android.util.Log;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.digitalchina.lxnsy.R;

import org.lzh.framework.updatepluginlib.creator.InstallCreator;
import org.lzh.framework.updatepluginlib.model.Update;
import org.lzh.framework.updatepluginlib.util.SafeDialogOper;

public class CustomNeedInstallCreator extends InstallCreator {

    @Override
    public Dialog create(final Update update, final String path, Activity activity) {
        if (activity == null || activity.isFinishing()) {
            Log.e("DownDialogCreator--->","show install dialog failed:activity was recycled or finished");
            return null;
        }
        String updateContent = activity.getText(org.lzh.framework.updatepluginlib.R.string.update_version_name)
                + ": " + update.getVersionName() + "\n\n\n"
                + update.getUpdateContent();

        MaterialDialog.Builder builder = new MaterialDialog.Builder(activity)
                .title(R.string.update_install_title)
                .content(updateContent)
                .positiveText(R.string.update_install_immediate)
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        if (!update.isForced()) {
                            SafeDialogOper.safeDismissDialog(dialog);
                        }
                        sendToInstall(path);
                    }
                });

        if (!update.isForced() && update.isIgnore()) {
            builder.negativeText(R.string.update_ignore)
                    .onNegative(new MaterialDialog.SingleButtonCallback() {
                        @Override
                        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                            sendCheckIgnore(update);
                            SafeDialogOper.safeDismissDialog(dialog);
                        }
                    });
        }

        if (!update.isForced()) {
            builder.negativeText(R.string.update_cancel)
                    .onNegative(new MaterialDialog.SingleButtonCallback() {
                @Override
                public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                    sendUserCancel();
                    SafeDialogOper.safeDismissDialog(dialog);
                }
            });
        }

        return builder.build();
    }

}
